const BaseHandler     = require('../base-handler');
const HTTP_VERBS      = require('../../../constants').HTTP_VERBS;

class AddAdministratorHandler extends BaseHandler {

    get pattern() {
        return '/me/walls/:wall_id/administrators';
    }

    doPOST(request, response) {
        if(!request.session.user.authenticated) {
            response.end(JSON.stringify({
                success: false,
                error: "未登录，请登录后再试！"
            }));
        } else {
            var wallId = request.params.wall_id;
            var wallUserId = request.body.user_id;
            var systemUserId = request.session.user.id;
            if(request.query.role == 'author') {
                this.Controller.addWallAuthor(wallId, wallUserId, systemUserId, (result) => {
                    response.json(result);
                })
            } else {
                this.Controller.addWallAdministrator(wallId, wallUserId, systemUserId, (result) => {
                    response.json(result);
                });
            }
        }
    }

    setup() {
        this.registerVerb(HTTP_VERBS.POST, this.doPOST);
    }
}

module.exports = AddAdministratorHandler;